package com.edu.study.base.wordLibrary.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.edu.study.base.wordLibrary.domain.wordLibrary;
import com.edu.study.base.wordLibrary.mapper.wordLibraryMapper;
import com.edu.study.base.wordLibrary.service.IwordLibraryService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

/**
 * wordLibraryService业务层处理
 * 
 * @author zw
 * @date 2021-06-26
 */
@Service
//@DataSource(value = DataSourceType.SLAVE)
public class wordLibraryServiceImpl implements IwordLibraryService 
{
    private static final Logger log = LoggerFactory.getLogger(wordLibraryServiceImpl.class);
    @Autowired
    private wordLibraryMapper wordLibraryMapper;

    /**
     * 查询wordLibrary
     * 
     * @param id wordLibraryID
     * @return wordLibrary
     */
    @Override
    public wordLibrary selectwordLibraryById(String id)
    {
        return wordLibraryMapper.selectwordLibraryById(id);
    }

    @Override
    public wordLibrary selectwordLibraryByName(String word) {
        return wordLibraryMapper.selectwordLibraryByName(word);
    }

    /**
     * 查询wordLibrary列表
     * 
     * @param wordLibrary wordLibrary
     * @return wordLibrary
     */
    @Override
    public List<wordLibrary> selectwordLibraryList(wordLibrary wordLibrary)
    {
        return wordLibraryMapper.selectwordLibraryList(wordLibrary);
    }

    @Override
    public List<wordLibrary> selectCourseWord(wordLibrary wordLibrary) {
        return wordLibraryMapper.selectCourseWord(wordLibrary);
    }

    @Override
    public String importWord(List<wordLibrary> wordLibraryList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(wordLibraryList) || wordLibraryList.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successAddNum = 0;
        int successUpdateNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for(wordLibrary word : wordLibraryList){
            try{
                wordLibrary w = this.selectwordLibraryByName(word.getWord());
                if(StringUtils.isNull(w)){
                    word.setCreateBy(operName);
                    word.setCreateTime(new Date());
                    this.insertwordLibrary(word);
                    successAddNum++;
                    //successMsg.append("<br/>" + successNum + "、单词 " + word.getWord() + " 导入成功");
                }else{
                    word.setId(w.getId());
                    word.setUpdateBy(operName);
                    word.setUpdateTime(new Date());
                    this.updatewordLibrary(word);
                    successUpdateNum++;
                    //successMsg.append("<br/>" + successNum + "、单词 " + word.getWord() + " 更新成功");

                }

            }catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum + "、单词 " + word.getWord() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);

            }

        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！<br/>新增了 " + successAddNum + " 条，<br/>更新了 " + successUpdateNum + " 条");
        }
        return successMsg.toString();
    }

    /**
     * 新增wordLibrary
     * 
     * @param wordLibrary wordLibrary
     * @return 结果
     */
    @Override
    public int insertwordLibrary(wordLibrary wordLibrary)
    {
        wordLibrary.setId(IdUtils.simpleUUID());
        return wordLibraryMapper.insertwordLibrary(wordLibrary);
    }

    /**
     * 修改wordLibrary
     * 
     * @param wordLibrary wordLibrary
     * @return 结果
     */
    @Override
    public int updatewordLibrary(wordLibrary wordLibrary)
    {
        return wordLibraryMapper.updatewordLibrary(wordLibrary);
    }

    /**
     * 批量删除wordLibrary
     * 
     * @param ids 需要删除的wordLibraryID
     * @return 结果
     */
    @Override
    public int deletewordLibraryByIds(String[] ids)
    {
        return wordLibraryMapper.deletewordLibraryByIds(ids);
    }

    /**
     * 删除wordLibrary信息
     * 
     * @param id wordLibraryID
     * @return 结果
     */
    @Override
    public int deletewordLibraryById(String id)
    {
        return wordLibraryMapper.deletewordLibraryById(id);
    }
 
}
